﻿using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Arich.Data.GenericNHDal;
using NHibernate.Cfg;
using NHibernateDemo.DAL.Entity.ver2;
using NHibernate;
namespace NHibernateDemo.DAL.Test {
    [TestClass]
    public class NHverUnitTest {
        private ISessionFactory sessionFactory;

        [TestInitialize]
        public void Setup( ) {
            log4net.Config.XmlConfigurator.Configure(new FileInfo("log4net.config"));
            HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize( );
            sessionFactory = (new Configuration( )).Configure( ).BuildSessionFactory( );
            
        }

        [TestMethod]
        public void SelectFundBaisc2Test( ) {
            FundBasicDAL2 target = new FundBasicDAL2( );
            var list = target.GetAll( ).Where(f => f.ShareClasses.Count > 0).Take(10).ToList( );
            ShowDetail(list);


        }

        [TestMethod]
        public void SelectFundBasic2Test2() {
            FundBasicDAL2 target = new FundBasicDAL2();
            var list = target.SearchByDomicile("USA");
            ShowDetail(list.ToList());
        }

        [TestMethod]
        public void SelectFundBasic2lv2CacheTest() {
            FundBasicDAL2 target = new FundBasicDAL2( );
            var list0 = target.SearchByDomicile("USA");
            //ShowDetail(list0.ToList());
            //NHibernateSessionManager.ResetFactory();
            //NHibernateSessionManager.Instance.GetSession( ).Close( );

            var list = target.SearchByDomicile("USA");
            Console.WriteLine(NHibernateSessionManager.Instance.Statistics.QueryExecutionCount);
            Console.WriteLine(NHibernateSessionManager.Instance.Statistics.QueryCachePutCount);
            Console.WriteLine(NHibernateSessionManager.Instance.Statistics.QueryCacheHitCount);
            ShowDetail(list.ToList());
            NHibernateSessionManager.Instance.GetSession( ).Close( );
            //target.GetById("000GEN");
           
            target.GetById(list[0].FundKey);
           
            //ISession session = sessionFactory.OpenSession( );
            //var list0 = session.CreateQuery("from FundBasic2 f where f.Domicile = :name").SetString("name", "USA").SetCacheable(true).List<FundBasic2>( );
            //session.Close( );
            //session = sessionFactory.OpenSession( );
            //var list = session.CreateQuery("from FundBasic2 f where f.Domicile = :name").SetString("name", "USA").SetCacheable(true).List<FundBasic2>( ).ToList( );
            //session.Close( );
            //session = sessionFactory.OpenSession( );
            //session.Get<FundBasic2>(list[0].FundKey);
            //session.Close( );
        }

        private static void ShowDetail(List<FundBasic2> list) {
            Assert.IsTrue(list.Count > 0);
            list.ForEach(f => Console.WriteLine(string.Format("{0},{1},{2},{3}", f.FundId, f.FundKey, f.FundStatusId,
                                                              f.FundTypeId)));
            var fund = list[0];
            Assert.IsTrue(fund.ShareClasses.Count > 0);
            var share = fund.ShareClasses.ToList()[0];
            foreach (var s in fund.ShareClasses) Console.WriteLine(s.ShareClassName);

            Console.WriteLine(share.Type.Id + "," + share.Type.Description);
        }
    }
}
